#include "LaserDoor.h"
#include "Room.h"
#include "DestroyEntityMessage.h"
#include "MessageSystem.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "GamePlayState.h"

unsigned int LaserDoor::Count = 0;
int LaserDoor::ImageID = -1;

// Constructor
LaserDoor::LaserDoor(Room* roomToLock, bool vertical) : Vert(vertical), LockedRoom(0), done(false)
{ 
	if( ImageID == -1 ) // Load the static image for all laser doors
		ImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/LaserDoor.png"),D3DCOLOR_XRGB(0,255,0));

	LockedRoom = roomToLock;

	SetTextureID(ImageID);

	if( Vert )
	{
		SetWidth(32);
		SetHeight(96);
	}
	else
	{
		SetWidth(96);
		SetHeight(32);
	}

	Count++;
}

// Destructor
LaserDoor::~LaserDoor(void) 
{
	if( --Count == 0 )
	{	// Unload the laser door image
		CSGD_TextureManager::GetInstance()->UnloadTexture(ImageID);
		ImageID = -1;
	}
}

// Update
void LaserDoor::Update(float elapsed)
{
	int camx = GamePlayState::GetInstance()->GetCamX();
	int camy = GamePlayState::GetInstance()->GetCamY();
	if( GetX() > camx + 800 || GetX() + GetWidth() < camx || GetY() + GetHeight() < camy || GetY() > camy + 600 )
	{
		return;
	}

	if( LockedRoom != 0 )
	{
		if( LockedRoom->GetEnemyVec().size() == 0 && LockedRoom->spawned )
		{	
			SetWidth(0);
			SetHeight(0);
			done = true;
		}
	}
}

// Draw
void LaserDoor::Render(void)
{
	// Get the camera
	int camx = GamePlayState::GetInstance()->GetCamX();
	int camy = GamePlayState::GetInstance()->GetCamY();

	if( done )
		return;

	if( Vert ) // Adjust image placement for side doorways
		CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),(int)GetX() + 32 - camx,(int)GetY() - camy,1,1,0,0,0,SGD_PI/2);
	else
		CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),(int)GetX() - camx,(int)GetY() - camy);
}

// Check collision
bool LaserDoor::CheckCollision(IEntity* base)
{
	return false;
}